package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PictureMapper;
import com.itheima.dao.PictureSortMapper;
import com.itheima.pojo.Picture;
import com.itheima.service.PictureService;
import com.itheima.utils.IdWorker;
import com.itheima.pojo.Comment;
import com.itheima.pojo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    private PictureMapper pictureMapper;

    @Autowired
    private PictureSortMapper pictureSortMapper;

    /**
     *添加图片
     * @param searchMap
     */
    @Override
    public void add(PictureVO pictureVO) {
        Picture picture = JSON.parseObject(JSON.toJSONString(pictureVO), Picture.class);
        //生成id
        IdWorker idWorker=new IdWorker();
        String id = idWorker.nextId()+"";
        picture.setUid(id);
        pictureMapper.insert(picture);
    }

    /**
     * 删除图片
     * @param pictureVO
     */
    @Override
    public void delete(PictureVO pictureVO) {
        Example example = createExample(pictureVO);
        pictureMapper.deleteByExample(example);
    }

    /**
     * 编辑图片
     * @param pictureVO
     */
    @Override
    public void edit(PictureVO pictureVO) {
        Picture picture = JSON.parseObject(JSON.toJSONString(pictureVO), Picture.class);
        //根据uid查询需要编辑的对象
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(pictureVO!=null) {
            if (pictureVO.getUid() != null && !"".equals(pictureVO.getUid())) {
                criteria.andEqualTo("uid", pictureVO.getUid());
            }
        }
        pictureMapper.updateByExample(picture,example);
    }

    /**
     * 获取图片列表
     * @param pictureVO
     * @return
     */
    @Override
    public PageInfo<Picture> getList(PictureVO pictureVO) {
        Integer currentPage = pictureVO.getCurrentPage();
        Integer pageSize = pictureVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(pictureVO);
        List<Picture> pictures = pictureMapper.selectByExample(example);
       return new PageInfo<>(pictures);
    }

    /**
     * 通过图片Uid将图片设为封面
     * @param pictureVO
     */
    @Override
    public void setCover(PictureVO pictureVO) {
        Example example = createExample(pictureVO);
        List<Picture> pictures = pictureMapper.selectByExample(example);
        for (Picture picture : pictures) {
            //更新
            pictureSortMapper.setCover(picture.getFileUid(),picture.getUid());
        }
    }

    private Example createExample(PictureVO pictureVO) {
        Example example=new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        if(pictureVO!=null){
            // 图片uid
            if (pictureVO.getFileUid() != null && !"".equals(pictureVO.getFileUid())) {
                criteria.andEqualTo("fileUid",pictureVO.getFileUid());
            }

         /*   // 图片uids
            if (pictureVO.getFileUids() != null && !"".equals(pictureVO.getFileUids())) {
                criteria.andEqualTo("fileUids", pictureVO.getFileUids());
            }*/

            // 图片名
            if (pictureVO.getPicName() != null && !"".equals(pictureVO.getPicName())) {
                criteria.andLike("picName", "%"+pictureVO.getPicName()+"%");
            }
            // 分类uid
            if (pictureVO.getPictureSortUid() != null && !"".equals(pictureVO.getPictureSortUid())) {
                criteria.andEqualTo("pictureSortUid", pictureVO.getPictureSortUid());
            }
            // 状态
            if (pictureVO.getStatus() != null && !"".equals(pictureVO.getStatus())) {
                criteria.andEqualTo("status", pictureVO.getStatus());
            }
            // 主键
            if (pictureVO.getUid() != null && !"".equals(pictureVO.getUid())) {
                criteria.andEqualTo("uid", pictureVO.getUid());
            }
        }
        return example;
    }
}
